package dj.action;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import dj.Constant;
import dj.action.base.BaseAction;
import dj.model.Friend;
import dj.model.FriendExtend;
import dj.model.FriendGroup;
import dj.model.Tuser;
import dj.pageModel.Json;
import dj.pageModel.Pfriend;
import dj.pageModel.Puser;
import dj.pageModel.TreeNode;
import dj.service.FriendService;
import dj.service.UserService;

@Action("friendAction")
public class FriendAction extends BaseAction implements ModelDriven<Pfriend> {

	private Pfriend pfriend = new Pfriend();

	private FriendService friendService;

	private UserService userService;

	public Pfriend getModel() {
		return pfriend;
	}

	public FriendService getFriendService() {
		return friendService;
	}

	@Autowired
	public void setFriendService(FriendService friendService) {
		this.friendService = friendService;
	}

	public UserService getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void getFriend() {
		Puser puser = (Puser) ServletActionContext.getRequest().getSession().getAttribute(Constant.USER_SESSION);
		List<TreeNode> tree = new ArrayList<TreeNode>();
		try {
			if (null != puser) {
				List<Friend> ls = friendService.getFriendListByGroupid(puser, pfriend.getGroupid());
				if (null != ls) {
					tree = friendService.getFriendTree(ls);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.writeToJson(tree);
	}

	public void getFriendGroup() {
		Puser puser = (Puser) ServletActionContext.getRequest().getSession().getAttribute(Constant.USER_SESSION);
		List<FriendGroup> ls = new ArrayList<FriendGroup>();
		if (null != puser) {
			try {
				ls = friendService.getFriendGroup(puser.getId());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		super.writeToJson(ls);
	}

	public void addFriend() {
		Puser puser = (Puser) ServletActionContext.getRequest().getSession().getAttribute(Constant.USER_SESSION);
		if(!puser.getId().equals(pfriend.getFriendid())) {
			Tuser friend = userService.getUserById(pfriend.getFriendid());
			// 判断人员是否在好友组中
			Friend f = friendService.getFriend(puser.getId(), pfriend.getFriendid());

			if (null == f) {
				f = new Friend();
				f.setId(UUID.randomUUID().toString());
				f.setUserid(puser.getId());
				f.setTuser(friend);
				f.setGroupid(pfriend.getGroupid());
				if (null != pfriend.getPfriendExtend().getRemarkName() && !"".equals(pfriend.getPfriendExtend().getRemarkName())) {
					FriendExtend fe = new FriendExtend();
					fe.setFriend(f);
					fe.setId(UUID.randomUUID().toString());
					fe.setRemarkName(pfriend.getPfriendExtend().getRemarkName());
					f.getFriendExtends().add(fe);
				}
				friendService.saveFriend(f);
				TreeNode tn = friendService.getFriendNode(f);
				super.writeToJson(tn);
			}
		}
		
		super.writeToJson(null);
	}
	
	public void updateFreindRemarkName() {
		Json j = new Json();
		Puser puser = (Puser) ServletActionContext.getRequest().getSession().getAttribute(Constant.USER_SESSION);
		Friend f = friendService.getFriend(puser.getId(), pfriend.getFriendid());
		try{
			if (null != f.getFriendExtends() && f.getFriendExtends().size() > 0) {
				FriendExtend fe = f.getFriendExtends().iterator().next();
				fe.setRemarkName(pfriend.getPfriendExtend().getRemarkName());
			} else {
				FriendExtend fe = new FriendExtend();
				fe.setFriend(f);
				fe.setId(UUID.randomUUID().toString());
				fe.setRemarkName(pfriend.getPfriendExtend().getRemarkName());
				f.getFriendExtends().add(fe);
			}
			friendService.updateFriend(f);
			j.setSuccess(true);
		} catch(Exception e) {
			j.setMsg("备注信息修改失败！");
			e.printStackTrace();
		}
		super.writeToJson(j);
	}
	
	public void deleteFriend() {
		Json j = new Json();
		Puser puser = (Puser) ServletActionContext.getRequest().getSession().getAttribute(Constant.USER_SESSION);
		Friend f = friendService.getFriend(puser.getId(), pfriend.getFriendid());
		try{
			friendService.deleteFriend(f);
			j.setSuccess(true);
			j.setMsg("好友已删除");
		} catch(Exception e) {
			j.setMsg("好友删除失败");
			e.printStackTrace();
		}
		super.writeToJson(j);
	}

	public void isFriend() {
		Json j = new Json();
		Puser puser = (Puser) ServletActionContext.getRequest().getSession().getAttribute(Constant.USER_SESSION);
		// 判断人员是否在好友组中
		Friend f = friendService.getFriend(puser.getId(), pfriend.getFriendid());
		if (null == f) {
			j.setSuccess(true);
		} else {
			j.setMsg("(" + f.getTuser().getName() + ")已经是您的好友，不能重复添加！");
		}
		super.writeToJson(j);
	}

}
